<template>
  <div class="editorUser-contanier">
    <div class="editorUser-header">
      <h3>{{newsParams.newsType|infoTitle}}</h3>
    </div>
    <div class="editorUser-content">
      <el-table ref="singleTable" border :data="newsLists.Data" highlight-current-row style="width: 100%">
        <el-table-column type="index" width="50" :index="indexMethod">
        </el-table-column>
        <el-table-column property="Title" label="标题">
        </el-table-column>
        <el-table-column label="发布状态" width="90">
          <template slot-scope="scope">
            <span v-if="scope.row.Status==0">公开</span>
            <span v-if="scope.row.Status==1">内部</span>
            <span v-if="scope.row.Status==2">未审</span>
          </template>
        </el-table-column>
        <el-table-column label="发布日期" width="230">
          <template slot-scope="scope">
            <span>{{scope.row.CreatedAt}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="editorUser-page" v-if="count>pageSize+1">
        <el-pagination @current-change="handleCurrentChange" :current-page.sync="currentPage3" :page-size="pageSize" layout="prev, pager, next, jumper" :total="count">
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>

export default {
  data() {
    return {
      searchKey: '',
      titles: ['节能动态', '通知公告', '情况通报', '政策法规', '节能知识'],
      newsLists: {},
      currentPage3: 1,
      count: 0,
      pageIndex: 1,
      pageSize: 12,
      newsParams: {
        newsType: this.$route.params.newsType
      }
    }
  },
  filters: {
    infoTitle: function(val) {
      return {
        1: '节能动态列表',
        2: '通知公告列表',
        3: '情况通报列表',
        4: '政策法规列表',
        5: '节能知识列表'
      }[val]
    }
  },
  beforeRouteUpdate(to, from, next) {
    this.newsParams = to.params
    this.count = 0
    this.pageIndex = 1
    this.currentPage3 = 1
    this.getNewsList()
    next()
  },
  methods: {
    // 序列号
    indexMethod(index) {
      return this.pageSize * (this.pageIndex - 1) + index + 1
    },
    // 页码切换事件
    handleCurrentChange(val) {
      this.pageIndex = val
      this.currentPage3 = val
      this.getNewsList()
      console.log(`当前页: ${val}`);
    },
    // 显示用户编辑页面
    handleEdit(row) {
      this.$router.push({ path: `/addInfo/${this.newsParams.newsType}/${row.Id}` })
    },
    // 删除信息
    handleDelete(item) {
      console.log(item)
      this.$confirm(`此操作将删除该条信息记录, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          const result = await this.$delete('/api/News/' + item.Id)
          this.$message({
            type: 'success',
            message: '删除成功'
          });
          this.getNewsList()
        } catch (error) {
          this.$message.error(error.message)
        }
      }).catch(() => { })
    },
    // 获取信息列表
    async getNewsList() {
      const category = this.titles[this.newsParams.newsType - 1];
      this.newsLists = []
      const result = await this.$get('/api/News', { category: category, pageSize: this.pageSize, pageIndex: this.pageIndex })
      this.newsLists = result
      console.log(this.newsLists, '用户列表')
      this.count = result.Count
    }
  },
  created() {
    this.showHome.isShow = false;
    this.getNewsList()
  }
}

</script>
<style lang="scss" scoped>
.editorUser-contanier {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.editorUser-header {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  h3 {
    height: 50px;
    line-height: 50px;
    font-size: 16px;
    margin-right: 10px;
  }
}

.editorUser-content {
  height: 550px;
  width: 100%;
  margin-top: 10px;
  padding: 0 15px;
}

.editorUser-page {
  margin-top: 15px;
  width: 100%;
  display: flex;
  justify-content: center;
}

.el-input {
  width: 200px;
  height: 25px;
  line-height: 25px;
}
</style>
